package com.icinfo.cloud.provider.punish.warning.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.HmacAuthUtil;
import com.icinfo.cloud.provider.common.utils.HttpUtils;
import com.icinfo.cloud.provider.peripheralinterface.xzzf.service.IXzzfInterfaceService;
import com.icinfo.cloud.provider.punish.warning.dto.AllClueDto;
import com.icinfo.cloud.provider.punish.warning.dto.CurrentClueDto;
import com.icinfo.cloud.provider.punish.warning.dto.WarnTokenDto;
import com.icinfo.cloud.provider.punish.warning.service.IEarlyWarningService;
import com.icinfo.cloud.provider.punish.warning.vo.WarnTokenVo;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.tools.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class EarlyWarningServiceImpl implements IEarlyWarningService {

    @Resource
    private IXzzfInterfaceService xzzfInterfaceService;

    /**
     *预警token获取
     * @author liyafeng
     * @date 2022/7/6
      * @param warnTokenDto
     * @return {@link WarnTokenVo}
     */
    @Override
    public WarnTokenVo getWarnToken(WarnTokenDto warnTokenDto) {
        WarnTokenVo warnTokenVo = new WarnTokenVo();
        String getTokenUrl="https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220614000001/api/enforcement/enforcement/supervision/warning/warnToken";
        String url=getTokenUrl+"?userRole=" + warnTokenDto.getUserRole() + "&userName=" + warnTokenDto.getUserName();
        String warningToken="";
        try {
            Map<String, String> headerMap= HmacAuthUtil.generateHeader(url, "GET", "68cb45609ce54263b25e85dcfc3ceaee", "e3c863ac294747d2aa8b5dbd181bdb72");
            Map<String, String> param = new HashMap<>();
            param.put("userRole", warnTokenDto.getUserRole());
            param.put("userName", warnTokenDto.getUserName());
            String result=HttpUtils.doGet(getTokenUrl,param,headerMap);
            JSONObject jsonObject = JSON.parseObject(result);
            String code = jsonObject.getString("code");
            if(StringUtils.isNotBlank(code)&& Constant.STRING_200.equals(code)){
                JSONObject data = jsonObject.getJSONObject("data");
                warningToken = data.getString("warnToken");
                log.info("warnToken:" + warningToken);
                warnTokenVo.setWarnToken(warningToken);

                //设置departmentCode 执法部门编码
                UserDTO userInfo = UserUtil.getUserInfo();
                log.info("部门编码departmentCode:" + userInfo.getDeptCode());
                warnTokenVo.setDepartmentCode(userInfo.getDeptCode());
                //设置executeUserCode 执法人员执法证号
                String lawEnforcementCert = xzzfInterfaceService.getLawEnforcementCertWithUserId(userInfo.getUserId().toString());
                log.info("执法证号executeUserCode:" + lawEnforcementCert);
                warnTokenVo.setExecuteUserCode(lawEnforcementCert);

                return warnTokenVo;
            }

        }catch (Exception e){
            log.warn("getWarnToken 异常=========>>>>> :{}", e.getMessage());
            throw new BusinessException("获取token失败！");
        }
        return null;
    }
    /**
     * 预警线索查询
     * @author liyafeng
     * @date 2022/7/6
      * @param allClueDto
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject getAllClue(AllClueDto allClueDto) {
        String urlStr = "https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220614000002/api/enforcement/enforcement/supervision/warning/all/clue";//.concat("?clueState=" + clueState + "&clueDateStart=" + clueDateStart+ "&clueDateEnd=" + clueDateEnd+ "&pageNo=" + pageNo + "&pageSize=" + pageSize);
        Map<String, String> stringStringMap = HmacAuthUtil.generateHeader(urlStr, "POST", "68cb45609ce54263b25e85dcfc3ceaee", "e3c863ac294747d2aa8b5dbd181bdb72");
        JSONObject jsonObject=new JSONObject();
        try {
            Map<String,String> param = new  HashMap<>();
            param.put("clueState", allClueDto.getClueState());
            param.put("clueDateStart", allClueDto.getClueDateStart());
            param.put("clueDateEnd", allClueDto.getClueDateEnd());
            param.put("pageNo", allClueDto.getPageNo() + "");
            param.put("pageSize", allClueDto.getPageSize() + "");
            JSONObject result=HttpUtils.doHttpPostWithHeader(urlStr,param,stringStringMap);
            String code = result.getString("code");
            if (StringUtils.isNotBlank(code) && Constant.STRING_200.equals(code)) {
                jsonObject = result.getJSONObject("data");
                return jsonObject;
            }
        }catch (Exception e){
            log.warn("getAllClue 异常=========>>>>> :{}", e.getMessage());
            throw new BusinessException("预警线索查询失败！");
        }
        return null;
    }
    /**
     * 获取当前待办预警线索
     * @author liyafeng
     * @date 2022/7/6
      * @param currentClueDto
     * @return {@link JSONObject}
     */
    @Override
    public JSONObject getCurrentClue(CurrentClueDto currentClueDto) {
        //设置departmentCode 执法部门编码
        UserDTO userInfo = UserUtil.getUserInfo();
        log.info("部门编码departmentCode:" + userInfo.getDeptCode());
        //设置executeUserCode 执法人员执法证号
        String lawEnforcementCert = xzzfInterfaceService.getLawEnforcementCertWithUserId(userInfo.getUserId().toString());
        log.info("执法证号executeUserCode:" + lawEnforcementCert);
        String url="https://bcdsg.zj.gov.cn:8443/restapi/prod/IC33000020220614000003/api/enforcement/enforcement/supervision/warning/clue/current";
        String urlStr = url.concat("?warnToken=" + currentClueDto.getWarnToken() +"&departmentCode="+userInfo.getDeptCode()+"&executeUserCode="+lawEnforcementCert+ "&pageNo=" + currentClueDto.getPageNo() + "&pageSize=" + currentClueDto.getPageSize());
        try {
            Map<String, String> stringStringMap = HmacAuthUtil.generateHeader(urlStr, "GET", "68cb45609ce54263b25e85dcfc3ceaee", "e3c863ac294747d2aa8b5dbd181bdb72");
            Map<String, String> param = new HashMap<>();
            param.put("warnToken", currentClueDto.getWarnToken());
            param.put("departmentCode", userInfo.getDeptCode());
            param.put("executeUserCode", lawEnforcementCert);
            param.put("pageNo", currentClueDto.getPageNo() + "");
            param.put("pageSize", currentClueDto.getPageSize() + "");
            String result=HttpUtils.doGet(url,param,stringStringMap);
            JSONObject resultObject = JSON.parseObject(result);
            String code = resultObject.getString("code");
            if(StringUtils.isNotBlank(code)&& Constant.STRING_200.equals(code)){
                JSONObject jsonObject = resultObject.getJSONObject("data");
                return jsonObject;
            }

        }catch (Exception e){
            log.warn("getCurrentClue 异常=========>>>>> :{}", e.getMessage());
            throw new BusinessException("获取当前待办预警线索失败！");
        }
        return null;
    }
    /**
     * 获取预警线索待办列表页面
     * @author liyafeng
     * @date 2022/7/6
      * @param warnToken
     * @return {@link String}
     */
    @Override
    public String getWarningClueCurrentPage(String warnToken) {
        String url="https://bcdsg.zj.gov.cn:8443/page/PC33000020220614000001/enforcement/enforcementTools/";
        String urlStr=url.concat("?warnToken=" + warnToken);
        Map<String, String> stringStringMap = HmacAuthUtil.generateHeader(urlStr, "GET","68cb45609ce54263b25e85dcfc3ceaee", "e3c863ac294747d2aa8b5dbd181bdb72");
        Map<String, String> param = new HashMap<>(1);
        param.put("warnToken", warnToken);
        String httpsGetHtml = HttpUtils.doGet(url, param, stringStringMap);
        return httpsGetHtml;
    }
    /**
     *预警线索反馈页面
     * @author liyafeng
     * @date 2022/7/6
      * @param warnToken
     * @return {@link String}
     */
    @Override
    public String getWarningClueHandlePage(String warnToken) {
        String url="https://bcdsg.zj.gov.cn:8443/page/PC33000020220614000002/enforcement/enforcementTools/details/";
        String urlStr=url.concat("?warnToken=" + warnToken);
        Map<String, String> stringStringMap = HmacAuthUtil.generateHeader(urlStr, "GET","68cb45609ce54263b25e85dcfc3ceaee", "e3c863ac294747d2aa8b5dbd181bdb72");
        Map<String, String> param = new HashMap<>(1);
        param.put("warnToken", warnToken);
        String httpsGetHtml = HttpUtils.doGet(url, param, stringStringMap);
        return httpsGetHtml;
    }


}
